package com.yjz.study.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.yjz.common.callback.ToastCallBack
import com.yjz.common.extend.showToast
import com.yjz.common.restful.CallBack
import com.yjz.common.restful.HttpBaseResponse
import com.yjz.study.account.AccountManager
import com.yjz.study.http.ApiFactory
import com.yjz.study.http.api.AccountApi
import com.yjz.study.http.entity.Resource
import kotlin.math.log

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/3/25 14:40
 */
class BizViewModel : ViewModel() {

    fun login(username: String, pwd: String): LiveData<Resource<String>> {
        val loginResult: MutableLiveData<Resource<String>> = MutableLiveData()
        loginResult.value = Resource(
            status = Resource.Status.LOADING,
            msg = ""
        )
        ApiFactory.create(AccountApi::class.java).login(username, pwd)
            .enqueue(object : CallBack<String> {
                override fun onSuccess(response: HttpBaseResponse<String>) {
                    if (response.successful()) {
                        loginResult.value = Resource(
                            status = Resource.Status.SUCCESS,
                            msg = response.msg!!,
                            data = response.data
                        )
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    loginResult.value = Resource(
                        status = Resource.Status.ERROR,
                        msg = throwable.message.toString()
                    )
                }
            })
        return loginResult
    }
}